package eclMain;
import java.util.HashMap;
import java.util.List;


public class Channel {
	//channel types
	public static final int OTHER = 0; //anything other than what's listed below
	public static final int CORP = 1;
	public static final int ALLIANCE = 2;
	public static final int MILITIA = 3;
	public static final int LOCAL = 4;
	public static final int CONSTELLATION = 5;
	public static final int FLEET = 6;
	public static final int INCURSION = 7;
	
	private String channelID = null;
	private int channelIDType = 0; //examples include "corpid", "allianceid", etc.
	private String channelName = null;
	private String listener = null;
	private TimeStamp sessionTimeStamp = null;
	
	private String location = null; //I'll put a system id or name here if the character changes systems. Might need to do this for other things like corp changes.
	//XXX: look up what happens when a character changes corps/alliances/militias while still logged in
	
	public Channel(List<ChatLine> list) {
		if (list == null) {
			this.sessionTimeStamp = new TimeStamp(null);
			return; //initialize a blank channel
		}
		parseHeader(list);
		this.location = channelID; //set initial location. If the channelIDType is "other" then this attribute won't matter anyways, because I'll have no need to look for it.
	}
	
	public void parseHeader(List<ChatLine> list) {
		HashMap<String, Integer> typeMap = new HashMap<String, Integer>();
		typeMap.put("corpid", Channel.CORP);
		typeMap.put("allianceid", Channel.ALLIANCE);
		typeMap.put("warfactionid", Channel.MILITIA);
		typeMap.put("solarsystemid", Channel.LOCAL);
		typeMap.put("constellationid", Channel.CONSTELLATION);
		typeMap.put("fleetid", Channel.FLEET);
		typeMap.put("incursion", Channel.INCURSION);
		
		for (ChatLine cl : list) {
			String line = cl.getEntireLine();
			if (line.trim().equals("---------------------------------------------------------------")) continue; //skip "header section" lines
			if (line.trim().length() <= 1) continue; //skip blank lines
			
			String[] lineArray = line.split(":", 2); //split into two pieces
			if (line.contains("Channel ID")) {
				if (lineArray[1].contains("(")) { //if it's a two-part ID
					lineArray[1] = lineArray[1].replace('(', ' ');
					lineArray[1] = lineArray[1].replace(')', ' ');
					lineArray[1] = lineArray[1].replace('\'', ' ');
					String[] idSplit = lineArray[1].split(",");
					String strID = idSplit[0].trim();
					for (String key : typeMap.keySet()) { //set channel type
						if (strID.contains(key)) {
							this.channelIDType = typeMap.get(key);
							break;
						}
					}
					this.channelID = idSplit[1].trim();
				} else { //if it's a 1-part ID
					this.channelID = lineArray[1].trim();
					this.channelIDType = Channel.OTHER;
				}
			}
			else if (line.contains("Channel Name")) this.channelName = lineArray[1].trim();
			else if (line.contains("Listener")) this.listener = lineArray[1].trim();
			else if (line.contains("Session started")) this.sessionTimeStamp = new TimeStamp(lineArray[1].trim());
		}
		if (isChannelInitialized() == false) {
			System.out.println();
		}
	}
	
	//return false if there is information missing from this channel
	public boolean isChannelInitialized() {
		if (getChannelID() == null) return false;
		if (getChannelName() == null) return false;
		if (getListener() == null) return false;
		if (getSessionTimeStamp() == null) return false;
		return true;
	}
	
	/**Note that the Channel ID sometimes contains letters as well as numbers*/
	public String getChannelID() {return this.channelID;}
	public void setChannelID(String channelID) {this.channelID = channelID;}
	
	public int getChannelIDType() {return this.channelIDType;}
	public void setChannelIDType(int channelIDType) {this.channelIDType = channelIDType;}
	
	public String getChannelName() {return this.channelName;}
	public void setChannelName(String channelName) {this.channelName = channelName;}
	
	public String getListener() {return this.listener;}
	public void setListener(String listener) {this.listener = listener;}
	
	public void setLocation(String loc) {this.location = loc;}
	public String getLocation() {return this.location;}
	
	public TimeStamp getSessionTimeStamp() {return this.sessionTimeStamp;}
	
	public String toString() {
		return this.channelID + "-" + this.channelIDType + "-" + this.channelName + "-" + this.listener;
	}
}
